<template>
  <div class="main">
    <a-form
      id="formLogin"
      class="user-layout-login"
      ref="formLogin"
      :model="formRef"
      @submit="handleSubmit"
    >
      <!-- 账户密码登录 -->

      <a-alert
        v-if="isLoginError"
        type="error"
        showIcon
        style="margin-bottom: 24px"
        :message="$t('user.login.message-invalid-credentials')"
      />
      <a-form-item v-bind="validateInfos.username">
        <a-input
          size="large"
          type="text"
          :placeholder="$t('user.login.username.placeholder')"
          v-model:value="formRef.username"
        >
          <template #prefix>
            <UserOutlined :style="{ color: 'rgba(0,0,0,.25)' }" />
          </template>
        </a-input>
      </a-form-item>

      <a-form-item v-bind="validateInfos.password">
        <a-input-password
          size="large"
          :placeholder="$t('user.login.password.placeholder')"
          v-model:value="formRef.password"
        >
          <template #prefix>
            <LockOutlined :style="{ color: 'rgba(0,0,0,.25)' }" />
          </template>
        </a-input-password>
      </a-form-item>

      <a-form-item v-bind="validateInfos.rememberMe">
        <a-checkbox v-model:checked="formRef.rememberMe" style="float: left">
          {{ $t("user.login.remember-me") }}
        </a-checkbox>
        <router-link
          :to="{ name: 'recover', params: { user: 'aaa' } }"
          class="forge-password"
          style="float: right"
          >{{ $t("user.login.forgot-password") }}</router-link
        >
      </a-form-item>

      <a-form-item style="margin-top: 24px">
        <a-button
          size="large"
          type="primary"
          htmlType="submit"
          class="login-button"
          :loading="state.loginBtn"
          :disabled="state.loginBtn"
          >{{ $t("user.login.login") }}</a-button
        >
      </a-form-item>
    </a-form>

    <two-step-captcha
      v-if="requiredTwoStepCaptcha"
      :visible="stepCaptchaVisible"
      @success="stepCaptchaSuccess"
      @cancel="stepCaptchaCancel"
    ></two-step-captcha>
  </div>
</template>

<script lang="ts">
import { defineComponent, onMounted, reactive, ref, UnwrapRef } from "vue";
import { useI18n } from "vue-i18n";
import { useRouter } from "vue-router";
// import md5 from "md5";
import { FormState } from "./types";
import TwoStepCaptcha from "@/components/tools/TwoStepCaptcha.vue";
import { useUserStore } from "@/store/modules/user";
import { timeFix } from "@/utils/util";
import { get2step } from "@/api/login";
import { notification } from "ant-design-vue";
import { useForm } from "ant-design-vue/lib/form";

export default defineComponent({
  components: {
    TwoStepCaptcha,
  },
  setup() {
    const router = useRouter();
    const userStore = useUserStore();
    const { t } = useI18n();
    // const Login = (userInfo) => store.dispatch('Login', userInfo)
    const Logout = userStore.Logout;

    onMounted(() => {
      get2step({})
        .then((res) => {
          // @ts-ignore
          requiredTwoStepCaptcha.value = res.result.stepCode;
        })
        .catch(() => {
          requiredTwoStepCaptcha.value = 0;
        });
      requiredTwoStepCaptcha.value = 1;
    });

    const isLoginError = ref(false);

    const state = reactive({
      time: 60,
      loginBtn: false,
      // login type: 0 email, 1 username, 2 telephone
      loginType: 0,
      smsSendBtn: false,
    });
    // #region 表单相关

    // handler
    const handleUsernameOrEmail = (rule, value) => {
      const regex =
        /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
      if (regex.test(value)) {
        state.loginType = 0;
      } else {
        state.loginType = 1;
      }
      return Promise.resolve("success");
    };

    const formRef: UnwrapRef<FormState> = reactive({
      rememberMe: false,
      username: "",
      password: "",
    });

    const rulesRef = reactive({
      rememberMe: [{ trigger: "checked" }],
      username: [
        {
          required: true,
          message: t("user.username.required"),
        },
        {
          validator: handleUsernameOrEmail,
          trigger: "change",
        },
      ],
      password: [{ required: true, message: t("user.password.required") }, {}],
    });

    /**
     * 登录成功后
     */
    const loginSuccess = (res?) => {
      router.push({ path: "/" });
      // 延迟 1 秒显示欢迎信息
      setTimeout(() => {
        const user = res ? `${res.username}` : "";
        notification.success({
          message: `欢迎${user}`,
          description: `${timeFix()}，欢迎回来`,
        });
      }, 1000);
    };

    const requestFailed = (err) => {
      console.log(err);
      notification["error"]({
        message: "错误",
        description:
          (err || err.data || {}).message || "请求出现错误，请稍后再试",
        duration: 4,
      });
    };

    //#region TwoStepCaptcha
    const requiredTwoStepCaptcha = ref<number>(0);
    const stepCaptchaVisible = ref<boolean>(false);
    const stepCaptchaSuccess = () => {
      loginSuccess();
    };
    const stepCaptchaCancel = () => {
      Logout().then(() => {
        state.loginBtn = false;
        stepCaptchaVisible.value = false;
      });
    };

    const { validate, validateInfos } = useForm(formRef, rulesRef);

    //#region 切换tab
    const customActiveKey = ref<string>("tab1");
    const handleTabClick = (key: string) => {
      customActiveKey.value = key;
    };
    //#endregion

    const handleSubmit = (e: Event) => {
      e.preventDefault();
      state.loginBtn = true;
      const validateFieldsKey = ["username", "password"];

      validate(validateFieldsKey)
        .then((result) => {          
          const loginParams = { ...result };        
          loginParams[ "username"] =
            result.username;
          // loginParams.password = md5(result.password);
          loginParams.password=result.password
          userStore
            .Login(loginParams)
            .then((res) => loginSuccess(res))
            .catch((err) => requestFailed(err))
            .finally(() => {
              state.loginBtn = false;
            });
        })
        .catch((err) => {
          console.log(err);
          setTimeout(() => {
            state.loginBtn = false;
          }, 600);
        });
    };



    return {
      formRef,
      rulesRef,
      state,
      customActiveKey,
      isLoginError,
      requiredTwoStepCaptcha,
      stepCaptchaVisible,
      handleTabClick,
      handleSubmit,
      validateInfos,
     
      stepCaptchaSuccess,
      stepCaptchaCancel,
    };
  },
});
</script>

<style lang="less" scoped>
@import "../../style/index.less";

.pro-layout-login-main {
  width: 360px;
  position: absolute;
  left: 50%;
  top: 30%;
  transform: translate(-50%, -50%);
}

.user-layout-login {
  label {
    font-size: 14px;
  }

  .forget-password {
    font-size: 14px;
  }

  button.login-button {
    padding: 0 15px;
    font-size: 16px;
    height: 40px;
    width: 100%;
  }

  .user-login-other {
    text-align: left;
    margin-top: 24px;
    line-height: 22px;

    .anticon {
      font-size: 24px;
      color: rgba(0, 0, 0, 0.2);
      margin-left: 16px;
      vertical-align: middle;
      cursor: pointer;
      transition: color 0.3s;

      &:hover {
        color: @primary-color;
      }
    }

    .register {
      float: right;
    }
  }
}
</style>
